-module(configuration_manager).



%% API
%%-export([init_mnesia/0]).


-export([do/1]).
%%-export([insert_app/1]).
%%-export([insert_instance/2]).
-export([insert/1]).
-export([delete/2]).
-export([delete/1]).
-export([lookup/2]).
-export([lookup_containers_by_app/1]).
-export([lookup_apps_by_container/1]).
-export([lookup_load_by_instance/2]).
-export([lookup_apps_deployed_container/1]).
-export([lookup_load_by_container/1]).




-include("xengine.hrl").
-include_lib("stdlib/include/qlc.hrl").




%%insert_instance(ContainerName , AppName) ->
%%	insert(#instance{container_name=ContainerName,app_name=AppName}).
	
	
	
%%insert_app(App)->
%%        insert(App).

%%lookup_app(Name)->
%%	lookup(app,Name).
%%lookup_container(Name)->
%%	lookup(container,Name).

lookup(instance , { ContainerName,AppName } )->
	do(qlc:q([X || X <- mnesia:table(instance), X#instance.container_name=:=ContainerName,X#instance.app_name=:=AppName]));
lookup(container_runtime,Name)->
	do(qlc:q([X || X <- mnesia:table(container_runtime), X#container_runtime.name=:=Name]));
lookup(app,Name)->
	do(qlc:q([X || X <- mnesia:table(app), X#app.name=:=Name]));
lookup(container,Name)->
	do(qlc:q([X || X <- mnesia:table(container), X#container.name=:=Name]));
lookup(instance_runtime,Instance)->
	do(qlc:q([X || X <- mnesia:table(instance_runtime), X#instance_runtime.instance=:=Instance])).

lookup_apps_deployed_container(ContainerName)->
        do(qlc:q([X || X <- mnesia:table(instance), X#instance.container_name=:=ContainerName, Y <- mnesia:table(app) , X#instance.app_name=:=Y#app.name])).
lookup_containers_by_app(AppName)->
      do(qlc:q([Y || X <- mnesia:table(instance), Y <- mnesia:table(container) ,      X#instance.container_name=:=Y#container.name,  X#instance.app_name=:=AppName])).
        
lookup_apps_by_container(ContainerName)->
         do(qlc:q([Y || X <- mnesia:table(instance), Y <- mnesia:table(app) ,      X#instance.app_name=:=Y#app.name,  X#instance.container_name=:=ContainerName])).
lookup_load_by_instance(AppName, ContainerName)->
	do(qlc:q([X#instance_runtime.runtime|| X <- mnesia:table(instance_runtime), X#instance_runtime.instance=:=#instance{container_name=ContainerName,app_name=AppName}])).
lookup_load_by_container(ContainerName) ->
	do(qlc:q([X#container_runtime.runtime||X <- mnesia:table(container_runtime), X#container_runtime.name=:=ContainerName])).

do(Q)->
	F = fun() -> qlc:e(Q) end,
	{atomic, Val }=mnesia:transaction(F),
	Val.



insert(Rows) when is_list(Rows) ->
	F = fun() ->
			lists:foreach(  fun mnesia:write/1 , Rows ) 
		end,
	case mnesia:transaction(F ) of
	   {atomic , ok }  ->
			ok;
	       Any ->
	                { error , Any}
	end;
insert(Row)->
	case mnesia:transaction(fun()-> mnesia:write(Row) end) of
		{atomic, ok }->
			ok  ;
	        Any ->
	                { error , Any}
	end.


delete(Tab , Key)->
        case mnesia:transaction(fun()-> mnesia:delete({Tab,Key}) end) of
		{atomic, ok }->
			ok  ;
                {aborted, Reason} ->
                        {error ,Reason }			
	end.

delete(Obj)->
        case mnesia:transaction(fun()-> mnesia:delete_object(Obj) end) of
		{atomic, ok }->
			ok  ;
                {aborted, Reason} ->
                        {error ,Reason }			
	end.

%%init_mnesia()->
	%%todo以后需要一个备用库	
%%	mnesia:create_schema([node()]),
%%	mnesia:start(),
%%	mnesia:create_table(container , [{attributes,record_info(fields,container)} , {disc_copies, node()} ]),
%%	mnesia:create_table(app , [{attributes,record_info(fields,app)},{disc_copies, node()}]),
	%%注意instance的唯一性约束
%%	mnesia:create_table(instance , [{attributes,record_info(fields,instance)},{disc_copies, node()} , {type, bag}]),
%%	mnesia:create_table(instance_runtime , [{attributes,record_info(fields,instance_runtime)},{disc_copies, node()}]).



